package service

import (
	"QHMedLink/model"

	"gorm.io/gorm"
)

type EquipmentPurchaseRepo struct {
	db *gorm.DB
}

func NewEquipmentPurchaseRepo(db *gorm.DB) *EquipmentPurchaseRepo {
	return &EquipmentPurchaseRepo{db: db}
}

// Create 创建记录
func (r *EquipmentPurchaseRepo) Create(purchase *model.EquipmentPurchase) error {
	return r.db.Create(purchase).Error
}

// GetByID 根据ID查询
func (r *EquipmentPurchaseRepo) GetByID(id uint) (*model.EquipmentPurchase, error) {
	var purchase model.EquipmentPurchase
	err := r.db.First(&purchase, id).Error
	return &purchase, err
}

// Update 更新记录
func (r *EquipmentPurchaseRepo) Update(purchase *model.EquipmentPurchase) error {
	return r.db.Save(purchase).Error
}

// Delete 删除记录
func (r *EquipmentPurchaseRepo) Delete(id uint) error {
	return r.db.Model(&model.EquipmentPurchase{}).
		Where("purchase_id = ?", id).
		Update("is_deleted", true).Error
}

// ListByCity 条件查询
func (r *EquipmentPurchaseRepo) ListByCity(city string) ([]model.EquipmentPurchase, error) {
	var purchases []model.EquipmentPurchase
	err := r.db.Where("city = ?", city).Find(&purchases).Error
	return purchases, err
}
